<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>微信登录</title>
</head>

<body>
<script>
    let GWC = {
        version: '1.1.1',
        urlParams: {},
        appendParams: function (url, params) {
            if (params) {
                let baseWithSearch = url.split('#')[0];
                let hash = url.split('#')[1];
                for (let key in params) {

                    let attrValue = params[key];
                    if (attrValue !== undefined) {
                        let newParam = key + "=" + attrValue;
                        if (baseWithSearch.indexOf('?') > 0) {
                            let oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');
                            if (oldParamReg.test(baseWithSearch)) {
                                baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
                            } else {
                                baseWithSearch += "&" + newParam;
                            }
                        } else {
                            baseWithSearch += "?" + newParam;
                        }
                    }
                }
                if (hash) {
                    url = baseWithSearch + '#' + hash;
                } else {
                    url = baseWithSearch;
                }
                console.log("finalUrl==", url)
            }
            return url;
        },
        getUrlParams: function () {
            let pairs = location.search.substring(1).split('&');
            for (let i = 0; i < pairs.length; i++) {
                let pos = pairs[i].indexOf('=');
                if (pos === -1) {
                    continue;
                }
                GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
            }
        },
        doRedirect: function () {
            let code = GWC.urlParams['code'];
            let appId = "${appId}";
            //let scope = GWC.urlParams['scope'] || 'snsapi_base';
            let scope = "${scope}";
            let state = "${state!''}";

            let redirectUri;
            if (!code) {


                //第一步，没有拿到code，跳转至微信授权页面获取code
                let url = location.href;
                if(url.indexOf("&state=")!=-1){
                    url = url.substring(0, url.indexOf("&state="))
                }


                redirectUri = GWC.appendParams('https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect', {
                    'appid': appId,
                    'redirect_uri': encodeURIComponent(url),
                    'response_type': 'code',
                    'scope': scope,
                    'state': state,
                });
                console.log("完整URL=",redirectUri)

            } else {

                //第二步，从微信授权页面跳转回来，已经获取到了code，再次跳转到实际所需页面
                redirectUri = GWC.appendParams("${url}", {
                    'code': code,
                    'state': state
                });

            }
            location.replace(redirectUri);
        }
    };
    GWC.getUrlParams();
    GWC.doRedirect();
</script>
</body>

</html>